<!-- 为ECharts准备一个具备大小（宽高）的Dom -->
<div id="mainTemplate" style="width: 600px;height:400px;float:left"></div>
<script src="/javascripts/echarts-all-3.js"></script>
<script type="text/javascript">
// 基于准备好的dom，初始化echarts实例
var myChart = echarts.init(document.getElementById('mainTemplate'));

// 指定图表的配置项和数据
var option = {
    title: {
        text: '折线图堆叠'
    },
    tooltip: {
        trigger: 'axis'
    },
    legend: {
        data:['useinit','utils.baseapp','__main__','setting','test']
    },
    grid: {
        left: '3%',
        right: '4%',
        bottom: '3%',
        containLabel: true
    },
    toolbox: {
        feature: {
            saveAsImage: {}
        }
    },
    xAxis: {
        type: 'category',
        boundaryGap: false,
        // data: ['周一','周二','周三','周四','周五','周六','周日']
        data:(function (){
                var now = new Date();
                var res = [];
                var len = 7;
                while (len--) {
                    res.unshift(now.toLocaleTimeString().replace(/^\D*/,''));
                    now = new Date(now - 1000);
                }
                return res;
            })()
    },
    yAxis: {
        type: 'value'
    },
    series: 
        // (function(){
        //    var array = new Array();
        //    var data = JSON.parse(sessionStorage.getItem("recent"));
        //    var obj = {};
        //    obj.data=new Array();
        //     obj.type='line';
        //     obj.stack="总量";
        //    for(i = 0; i < data.length; i++){
        //       obj.name=data[i].file;
        //       obj.data.push(data[i].count);
        //    }     
        //   array.push(obj)
        //    return array;
        // })(),
        [
        {
            name:'useinit',
            type:'line',
            stack: '总量',
            // data:[120, 132, 101, 134, 90, 230, 210]
            data:(function (){
                var res = [];
                var len = 7;
                var dataObj = JSON.parse(sessionStorage.getItem("recent"));
                while (len--) {
                    // res.push(Math.round(Math.random() * 1000));
                    //  var dataObj = JSON.parse(sessionStorage.getItem("data"));
                    //  var newData = new Array();
                    //  Object.keys(dataObj).forEach(function(i){
                    //       newData.push(dataObj[i]);
                    //  })
                    for(i = 0; i < 7; i++){
                        var files = dataObj[i].file;
                        if(files.indexOf("useinit") > 0){
                            var fileArray = files.split(',')
                            for(j = 0; j < fileArray.length; j++){
                                if(fileArray[j] == "useinit"){
                                    res.push(dataObj[i].count[j])
                                  
                                }
                            }
                        }else{
                              res.push(0)
                        }
                        if(res.length == 7){
                            console.log(res)
                            return res;
                        }
                    }
                }
                return res;
            })()
        },
        {
            name:'utils.baseapp',
            type:'line',
            stack: '总量',
            // data:[220, 182, 191, 234, 290, 330, 310]
            data:(function (){
                var res = [];
                var len = 7;
                var dataObj = JSON.parse(sessionStorage.getItem("recent"));
                 while (len--) {
                    // res.push(Math.round(Math.random() * 1000));
                    //  var dataObj = JSON.parse(sessionStorage.getItem("data"));
                    //  var newData = new Array();
                    //  Object.keys(dataObj).forEach(function(i){
                    //       newData.push(dataObj[i]);
                    //  })
                    for(i = 0; i < 7; i++){
                        var files = dataObj[i].file;
                        if(files.indexOf("utils.baseapp") > 0){
                            var fileArray = files.split(',')
                            for(j = 0; j < fileArray.length; j++){
                                if(fileArray[j] == "utils.baseapp"){
                                    res.push(dataObj[i].count[j])
                                  
                                }
                            }
                        }else{
                              res.push(0)
                        }
                        if(res.length == 7){
                            console.log(res)
                            return res;
                        }
                    }
                }
                return res;
            })()
        },
        {
            name:'__main__',
            type:'line',
            stack: '总量',
            // data:[150, 232, 201, 154, 190, 330, 410]
            data:(function (){
                var res = [];
                var len = 7;
                var dataObj = JSON.parse(sessionStorage.getItem("recent"));
                while (len--) {
                    // res.push(Math.round(Math.random() * 1000));
                    //  var dataObj = JSON.parse(sessionStorage.getItem("data"));
                    //  var newData = new Array();
                    //  Object.keys(dataObj).forEach(function(i){
                    //       newData.push(dataObj[i]);
                    //  })
                    for(i = 0; i < 7; i++){
                        var files = dataObj[i].file;
                        if(files.indexOf("__main__") > 0){
                            var fileArray = files.split(',')
                            for(j = 0; j < fileArray.length; j++){
                                if(fileArray[j] == "__main__"){
                                    res.push(dataObj[i].count[j])
                                }
                            }
                        }else{
                              res.push(0)
                        }
                        if(res.length == 7){
                            console.log(res)
                            return res;
                        }
                    }
                }
                return res;
            })()  
        },
        {
            name:'setting',
            type:'line',
            stack: '总量',
            // data:[320, 332, 301, 334, 390, 330, 320]
            data:(function (){
                var res = [];
                var len = 7;
                var dataObj = JSON.parse(sessionStorage.getItem("recent"));
                 while (len--) {
                    // res.push(Math.round(Math.random() * 1000));
                    //  var dataObj = JSON.parse(sessionStorage.getItem("data"));
                    //  var newData = new Array();
                    //  Object.keys(dataObj).forEach(function(i){
                    //       newData.push(dataObj[i]);
                    //  })
                    for(i = 0; i < 7; i++){
                        var files = dataObj[i].file;
                        if(files.indexOf("setting") > 0){
                            var fileArray = files.split(',')
                            for(j = 0; j < fileArray.length; j++){
                                if(fileArray[j] == "setting"){
                                    res.push(dataObj[i].count[j])
                                }
                            }
                        }else{
                              res.push(0)
                        }
                        if(res.length == 7){
                            console.log(res)
                            return res;
                        }
                    }
                }
                return res;
            })() 
        },
        {
            name:'test',
            type:'line',
            stack: '总量',
            // data:[820, 932, 901, 934, 1290, 1330, 1320]
            data:(function (){
                var res = [];
                var len = 7;
                var dataObj = JSON.parse(sessionStorage.getItem("recent"));
                while (len--) {
                    // res.push(Math.round(Math.random() * 1000));
                    //  var dataObj = JSON.parse(sessionStorage.getItem("data"));
                    //  var newData = new Array();
                    //  Object.keys(dataObj).forEach(function(i){
                    //       newData.push(dataObj[i]);
                    //  })
                    for(i = 0; i < 7; i++){
                        var files = dataObj[i].file;
                        if(files.indexOf("test") > 0){
                            var fileArray = files.split(',')
                            for(j = 0; j < fileArray.length; j++){
                                if(fileArray[j] == "test"){
                                    res.push(dataObj[i].count[j])
                                  
                                }
                            }
                        }else{
                              res.push(0)
                        }
                        if(res.length == 7){
                            console.log(res)
                            return res;
                        }
                    }
                }
                return res;
            })() 
        }

    ]
};
//   myChart.setOption(option);
  
setInterval(function (){
    axisData = (new Date()).toLocaleTimeString().replace(/^\D*/,'');
    var data = JSON.parse(sessionStorage.getItem("oneResult"))[0];

    var data0 = option.series[0].data;
    var data1 = option.series[1].data;
    var data2 = option.series[2].data;
    var data3 = option.series[3].data;
    var data4 = option.series[4].data;

    data0.shift();
    // var insertData0 = Math.round(Math.random() * 1000);
    var insertData0 = 0;
    var array = data.file.split(',');
    for(i = 0; i < array.length; i++){
        if(array[i] == "useinit"){
            insertData0 = data.count[i]
        }
    }
    data0.push(insertData0);
    data1.shift();
    var insertData1 = 0;
    var array = data.file.split(',');
    for(i = 0; i < array.length; i++){
        if(array[i] == "utils.baseapp"){
            insertData1 = data.count[i]
        }
    }
    data1.push(insertData1);
    data2.shift();
    var insertData2 = 0;
    var array = data.file.split(',');
    for(i = 0; i < array.length; i++){
        if(array[i] == "utils.baseapp"){
            insertData2 = data.count[i]
        }
    }
    data2.push(insertData2);
    data3.shift();
    var insertData3 = 0;
    var array = data.file.split(',');
    for(i = 0;i < array.length; i++){
        if(data.file.split(',')[i] == "utils.baseapp"){
            insertData3 = data.count[i]
        }
    }
    data3.push(insertData3);
    data4.shift();
    var insertData4 = 0;
     var array = data.file.split(',');
    for(i = 0; i < array.length; i++){
        if(data.file.split(',')[i] == "utils.baseapp"){
            insertData4 = data.count[i]
        }
    }
    data4.push(insertData4);

    var data = {"useinit":insertData0,"utils.baseapp":insertData1,"__main__":insertData2,"setting":insertData3,"test":insertData4};

  
    sessionStorage.setItem("data",JSON.stringify(data));

    option.xAxis.data.shift();
    option.xAxis.data.push(axisData);

    myChart.setOption(option);
}, 1000);
</script>